package com.bk.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/** 序列化对象 */
public class SerilizableObjectUtil {
	
	// 序列化 
	public static String serialize(Object obj) {
		ByteArrayOutputStream byteArrayOutputStream;
		ObjectOutputStream out;
		String objectStr ="";
		try {
			byteArrayOutputStream = new ByteArrayOutputStream();
			out = new ObjectOutputStream(byteArrayOutputStream);
			out.writeObject(obj);
			objectStr= byteArrayOutputStream.toString("ISO-8859-1");
			out.close();
	        byteArrayOutputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
        return objectStr;
	}

	// 反序列化
	public static Object unserizlize(String objectStr) {
		Object object = null;
		try {
			ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(objectStr.getBytes("ISO-8859-1"));
			ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
			object = objectInputStream.readObject();
			objectInputStream.close();
			byteArrayInputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
        return object;
	}
}
